home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Development Kits / MPW etc / MPW-GM / MPW / Examples / CFMExamples / ModApp / cfrg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-03  |  4.6 KB  |  159 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        cfrg.c
  3.  
  4.     Contains:    Converts a 'cfrg' resource into an easily understood form
  5.  
  6.     Written by:    Richard Clark
  7.  
  8.     Copyright:    © 1994 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.                  1/13/94    RC        Created
  13.  
  14.     To Do:
  15. */
  16.  
  17. #ifndef __MEMORY__
  18.     #include <Memory.h>
  19. #endif
  20.  
  21. #ifndef __RESOURCES__
  22.     #include <Resources.h>
  23. #endif
  24.  
  25. #ifndef __STDDEF__
  26.     #include <stddef.h>
  27. #endif
  28.  
  29. #ifndef __STRING__
  30.     #include <string.h>
  31. #endif
  32.  
  33. #include "cfrg.h"
  34.  
  35.  
  36. OSErr Parse_cfrg (Handle theResource, irHand internalCopy)
  37. {
  38.     // Copy the relevant items from the 'cfrg' resource into our internal format
  39.     SignedByte    oldResourceState, oldInternalState;
  40.     short        itemCount, index;
  41.     Ptr            itemStart;
  42.     long        headerSize = offsetof(cfrgHeader, arrayStart);
  43.     OSErr        err = noErr;
  44.  
  45.     // Lock our original resource up high so we can access it easily
  46.     oldResourceState = HGetState(theResource);
  47.     MoveHHi(theResource); HLock(theResource);
  48.     oldInternalState = HGetState(theResource);
  49.     
  50.     // set the size of the destination block
  51.     itemCount = (*(hdrHand)theResource)->itemCount;
  52.     SetHandleSize((Handle)internalCopy, offsetof(internalResource, itemList)
  53.                                         + itemCount * sizeof(internalItem));
  54.     err = MemError();
  55.     if (err) goto done;
  56.     HLock((Handle)internalCopy);
  57.     
  58.     // Copy the relevant fields from the header
  59.     (*internalCopy)->version = (*(hdrHand)theResource)->version;
  60.     (*internalCopy)->itemCount = itemCount;
  61.     
  62.     // Transfer each item from the cfrg into the internal resource
  63.     if (itemCount == 0) goto done;
  64.     itemStart = &(*(hdrHand)theResource)->arrayStart;
  65.     for (index = 0; index < itemCount; index++) {
  66.         cfrgItem*         srcItem;
  67.         internalItem*     dstItem;
  68.         
  69.         srcItem = (cfrgItem*)itemStart;
  70.         dstItem = &(*internalCopy)->itemList[index];
  71.         
  72.         dstItem->archType        = srcItem->archType;
  73.         dstItem->updateLevel    = srcItem->updateLevel;
  74.         dstItem->currVersion    = srcItem->currVersion;
  75.         dstItem->oldDefVersion    = srcItem->oldDefVersion;
  76.         dstItem->appStackSize    = srcItem->appStackSize;
  77.         dstItem->appSubFolder    = srcItem->appSubFolder;
  78.         dstItem->usage            = srcItem->usage;
  79.         dstItem->location        = srcItem->location;
  80.         dstItem->codeOffset        = srcItem->codeOffset;
  81.         dstItem->codeLength        = srcItem->codeLength;
  82.         BlockMove(srcItem->name, dstItem->name, srcItem->name[0]+1);
  83.         
  84.         itemStart += srcItem->itemSize;
  85.     }
  86.  
  87. done:
  88.     HSetState(theResource, oldResourceState);
  89.     HSetState((Handle)internalCopy, oldInternalState);
  90.     return err;    
  91. }
  92.  
  93. OSErr Build_cfrg (irHand internalCopy, Handle theResource)
  94. {
  95.     // Construct a cfrg resource from our internal template
  96.     SignedByte        oldResourceState, oldInternalState;
  97.     OSErr            err;
  98.     unsigned long    headerSize =  offsetof(cfrgHeader, arrayStart);
  99.     unsigned long    bytesCopied;
  100.     short            itemCount, index;
  101.  
  102.     // Construct the header by setting the destination handle to that
  103.     // size, clearing the memory, and then inserting the version and
  104.     // item count values
  105.     oldResourceState = HGetState(theResource);
  106.     oldResourceState = HGetState((Handle)internalCopy);
  107.     MoveHHi((Handle)internalCopy); HLock((Handle)internalCopy);
  108.     
  109.     SetHandleSize(theResource, headerSize);
  110.     err = MemError();
  111.     if (err) goto done;
  112.     memset(*theResource, 0, headerSize);
  113.     ((cfrgHeader*)(*theResource))->version = (*internalCopy)->version;
  114.     itemCount = (*internalCopy)->itemCount;
  115.     ((cfrgHeader*)(*theResource))->itemCount = itemCount;
  116.     
  117.     // Now, copy each item individually
  118.     bytesCopied = headerSize;
  119.     for (index = 0; index < itemCount; index++) {
  120.         cfrgItem*        dstPtr;
  121.         internalItem*    srcPtr;
  122.         long            itemSize;
  123.         unsigned long    newSize;
  124.         
  125.         // Calculate the size of this entry
  126.         srcPtr = &(*internalCopy)->itemList[index];
  127.         itemSize = offsetof(cfrgItem, name) + srcPtr->name[0] + 1;
  128.         itemSize += itemSize & 0x0003;    // Pad up to the next multiple of 4
  129.         
  130.         // Extend and clear the handle
  131.         newSize = bytesCopied + itemSize;
  132.         SetHandleSize(theResource, newSize);
  133.         err = MemError();
  134.         if (err) goto done;
  135.         dstPtr = (cfrgItem*)(((unsigned long)*theResource) + bytesCopied);
  136.         memset((Ptr)dstPtr, 0, itemSize);
  137.                 
  138.         // Transfer the individual fields
  139.         dstPtr->archType        = srcPtr->archType;
  140.         dstPtr->updateLevel        = srcPtr->updateLevel;
  141.         dstPtr->currVersion        = srcPtr->currVersion;
  142.         dstPtr->oldDefVersion    = srcPtr->oldDefVersion;
  143.         dstPtr->appStackSize    = srcPtr->appStackSize;
  144.         dstPtr->appSubFolder    = srcPtr->appSubFolder;
  145.         dstPtr->usage            = srcPtr->usage;
  146.         dstPtr->location        = srcPtr->location;
  147.         dstPtr->codeOffset        = srcPtr->codeOffset;
  148.         dstPtr->codeLength        = srcPtr->codeLength;
  149.         dstPtr->itemSize        = itemSize;
  150.         BlockMove(srcPtr->name, dstPtr->name, srcPtr->name[0] + 1);
  151.  
  152.         bytesCopied = newSize;
  153.     }
  154.  
  155. done:
  156.     HSetState(theResource, oldResourceState);
  157.     HSetState((Handle)internalCopy, oldInternalState);
  158.     return err;
  159. }